package com.hospital.new_yygh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.new_yygh.entity.other.Order;
import com.hospital.new_yygh.entity.other.Sche;
import com.hospital.new_yygh.entity.result.Result;
import com.hospital.new_yygh.entity.user.Doctor;
import com.hospital.new_yygh.entity.vo.ScheVo;
import com.hospital.new_yygh.service.ScheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "预约表")
@RestController
@RequestMapping("/sche")
@CrossOrigin
public class ScheController {
    //注入Service
    @Autowired
    private ScheService scheService;

    //分页查询排班信息
    @ApiOperation(value = "分页查询排班信息")
    @PostMapping("pageSche/{current}/{limit}")
    public Result pageSche(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false)ScheVo scheVo){
        //创建page对象，传递当前页，每页记录数
        Page<Sche> page = new Page<>(current, limit);
        QueryWrapper<Sche> wrapper = new QueryWrapper<>();
        String doctorName = scheVo.getDoctorName();
        String ksId = scheVo.getKsId();
        String workDate = scheVo.getWorkDate();
        Long doctorId = scheVo.getDoctorId();
        if (!StringUtils.isEmpty(doctorName)){
            wrapper.like("doctorName",doctorName);
        }
        if (!StringUtils.isEmpty(ksId)){
            wrapper.eq("ksId",ksId);
        }
        if (!StringUtils.isEmpty(workDate)){
            wrapper.apply("date_format(work_date,'%Y-%m-%d') = {0}",workDate);
        }
        if (!StringUtils.isEmpty(doctorId)){
            wrapper.eq("doctor_id",doctorId);
        }
        Page<Sche> pageSche = scheService.page(page, wrapper);
        return Result.ok(pageSche);
    }

    //根据id获取医生信息
    @ApiOperation(value = "根据id获取排班信息")
    @GetMapping("getSche/{id}")
    public Result getDoctor(@PathVariable Long id){
        Sche sche = scheService.getById(id);
        return Result.ok(sche);
    }

    //添加排班信息
    @ApiOperation(value = "添加排班信息")
    @PostMapping("saveSche")
    public Result saveSche(@RequestBody Sche sche){
        boolean save = scheService.save(sche);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //修改排班信息
    @ApiOperation(value = "修改排班信息")
    @PostMapping("updateSche")
    public Result updateSche(@RequestBody Sche sche){
        boolean b = scheService.updateById(sche);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //获取可预约的排班数据
    @ApiOperation(value = "获取可预约的排班数据")
    @PostMapping("findScheList/{current}/{limit}")
    public Result findScheList(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false)ScheVo scheVo){
        //创建page对象，传递当前页，每页记录数
        Page<Sche> page = new Page<>(current, limit);
        QueryWrapper<Sche> wrapper = new QueryWrapper<>();
        String ksId = scheVo.getKsId();
        String workDate = scheVo.getWorkDate();
        int status = scheVo.getStatus();
        if (!StringUtils.isEmpty(ksId)){
            wrapper.eq("ksId",ksId);
        }
        if (!StringUtils.isEmpty(workDate)){
            wrapper.apply("date_format(work_date,'%Y-%m-%d') = {0}",workDate);
        }
        wrapper.eq("status",1);
        Page<Sche> pageSche = scheService.page(page, wrapper);
        return Result.ok(pageSche);
    }


}
